﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LightCAD.MathLib.CSG
{
    internal static class Ext
    {
        public static List<T> makeSize<T>(this List<T> list,int size,T defaultValue=default(T))
        {
            list.Clear();
            for(var  i = 0; i < size; i++) 
            {
                list.Add(defaultValue);   
            }
            return list;
        }
        public static List<T> makeSize<T>(this List<T> list, int size, Func<T> func)
        {
            list.Clear();
            for (var i = 0; i < size; i++)
            {
                list.Add(func());
            }
            return list;
        }
        public static int size<T>(this List<T> list)
        {
            return list.Count;
        }
        public static List<T> resize<T>(this List<T> list,int newsize, T defaultValue = default(T))
        {
            var ds = newsize - list.Count;
            if (ds < 0)
            {
                for(var i = 0; i < Math.Abs(ds); i++)
                {
                    list.RemoveAt(list.Count-1);
                }
            }
            else
            {
                for (var i = 0; i < Math.Abs(ds); i++)
                {
                    list.Add(defaultValue);
                }
            }
            return list;
        }

        public static List<T> resize<T>(this List<T> list, int newsize, Func<T> func)
        {
            var ds = newsize - list.Count;
            if (ds < 0)
            {
                for (var i = 0; i < Math.Abs(ds); i++)
                {
                    list.RemoveAt(list.Count - 1);
                }
            }
            else
            {
                for (var i = 0; i < Math.Abs(ds); i++)
                {
                    list.Add(func());
                }
            }
            return list;
        }
        public static void push_back<T>(this List<T> list,T value)
        {
            list.Add(value);
        }
        public static void erase<T>(this List<T> list, T value)
        {
            list.Remove(value);
        }
        public static int counter<T>(this List<T> list,T value)
        {
            var count = 0;
            foreach(var  item in list)
            {
                if(Object.Equals(item,value)) 
                    count++;
            }
            return count;
        }
    }
}
